<template>
  <a-card :bordered="false">
  <!-- 欢迎语规则管理 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline"  @keyup.enter.native="getSegmentedData">
        <a-row :gutter="12">
          <a-col :md="6" :sm="8">
            <a-form-item label="规则名称" :labelCol="{ span: 6 }" :wrapperCol="{ span: 14, offset: 0 }">
              <a-input placeholder="请输入" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="状态" :labelCol="{ span: 6 }" :wrapperCol="{ span: 14, offset: 0 }">
              <a-select ref="select" v-model="queryParam.status" placeholder="状态" allowClear>
                <a-select-option :value="item.value" v-for="(item, index) in statusOption" :key="index">{{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="getSegmentedData" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>

          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button  @click="doudi" icon="search">兜底规则</a-button>
              <a-button  @click="handelecreat" icon="reload" style="margin-left: 8px">新建规则</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>

      <!-- <div class="table-operator" style="border-top: 5px">
        <a-button type="primary" @click="handleAdd">新增</a-button>
      </div> -->
      <a-tabs type="card" @change="callback"  :defaultActiveKey="queryParam.type">
        <a-tab-pane key="1" tab="门店码">
        </a-tab-pane>
        <a-tab-pane key="2" tab="导购码">
        </a-tab-pane>
        <a-tab-pane key="3" tab="企微导购码">
        </a-tab-pane>
      </a-tabs>

      <a-table
        ref="table"
        rowKey="id"
        size="middle"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange">
        <span slot="content" slot-scope="text, record">
          <a @click="xiaoxi(record)" style="text-decoration:underline;color:#000000A6;">
            {{record.content}}
          </a>
        </span>
        <span slot="action" slot-scope="text, record">
          <a @click="handleStatus(record)">
            <a-icon type=""/>
             {{record.status? '启用': '禁用'}}
          </a>
          <a-divider type="vertical"/>
          <a @click="handelecreat(record)">
            <a-icon type=""/>
            编辑
          </a>
          <a-divider type="vertical"/>
          <a @click="handelecreat(record,1)">
            <a-icon type=""/>
            复制
          </a>
          <a-divider type="vertical"  v-show="record.status == 1" />
          <a-popconfirm title="确定删除吗?" @confirm="() =>handleDelete(record.id)" v-show="record.status == 1">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </a-table>
    </div>

    <contentmodel ref="contentmodel" @ok="modalFormOk"></contentmodel> 
  </a-card>
</template>

<script>
// import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { saveOrUpdateWelcome, removeWelcomeById, pageWelcomeVo } from '@/api/marketingCenter/code'
import { filterObj } from '@/utils/util';
import contentmodel from './welcomewords/contentmodel'

export default {
  name:'wc',
  // mixins:[JeecgListMixin],
  components: { 
    contentmodel
  },
  data() {
    return {
      statusOption: [
        { value: "0", label: "启用" },
        { value: "1", label: "禁用" },
      ],
      queryParam:{
        type: 1,
        current: 1,
        size: 10,
      },
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        total: 0
      },
      loading:false,
      // 表头
      columns: [
        {
          title: '规则ID',
          align: "center",
          dataIndex: 'id',
        },
        {
          title: '规则名称',
          align: "center",
          dataIndex: 'name',
        },
        {
          title: '消息内容',
          align: 'center',
          dataIndex: 'content',
          scopedSlots: {customRender: 'content'},
        },
        {
          title: '规则状态',
          align: 'center',
          dataIndex: 'statusText',
        },
        {
          title: '操作',
          align: "center",
          dataIndex: 'action',
          scopedSlots: {customRender: 'action'},
        }
      ],
      dataSource:[],
      // url: {
      //   list: "/backend/qrcode/pageWelcomeVo",
      // },
    }
  },
  created() {
    if (this.$route.query.backType !== undefined) {
      console.log(this.$route.query.backType,'!== undefined');
      this.queryParam.type = this.$route.query.backType
    }
    this.getSegmentedData()
  },
  methods: {
    getSegmentedData() {
      this.queryParam.current = this.ipagination.current
      this.queryParam.size = this.ipagination.pageSize
      delete this.queryParam.pageSize
      this.loading = true
      pageWelcomeVo(this.queryParam).then(res => {
        if (res.code === 200) {
          this.dataSource = res.data.records
          this.ipagination.total = res.data.total;
        } else {
          this.$message.error(res.msg || res.message)
        }
        this.loading = false
      })
    },
    searchReset() {
      let type = this.queryParam.type
      this.queryParam = {
        type: type,
        current: 1,
        size: 10
      }
      this.getSegmentedData();
    },
    callback(key) {
      this.queryParam.type = key
      this.getSegmentedData()
    },
    //分页、排序、筛选变化时触发
    handleTableChange(pagination, filters, sorter) {
      //TODO 筛选
      console.log(pagination)
      if (Object.keys(sorter).length > 0) {
        this.isorter.column = sorter.field
        this.isorter.order = 'ascend' == sorter.order ? 'asc' : 'desc'
      }
      this.ipagination = pagination
      this.getSegmentedData()
    },
    xiaoxi(record) {
      this.$refs.contentmodel.edit(record);
      this.$refs.contentmodel.title = "";
      this.$refs.contentmodel.disableSubmit = false;
    },
    handleStatus(record) {
      const that = this
      this.$confirm({
        title: '确认要'+ `${record.status?'启用':'禁用'}`+'该门店码规则吗？',
        content: record.status? '请确保已和业务部门沟通一致':'禁用后将默认使用兜底规则',
        closable:true,
        okText: record.status? '启用':'禁用',
        cancelText: '取消',
        icon:'',
        onOk() {
          let status = ''
          let statusText = ''
          if (record.status == 1) {
            status = 0
            statusText = '启用'
          } else {
            status = 1
            statusText = '禁用'
          }
          saveOrUpdateWelcome({
            status:status,
            statusText:statusText,
            id:record.id
          }).then(res => {
            if(res.code == 200) {
              that.$message.success(res.msg)
              that.getSegmentedData();
            }
          })
        },
        onCancel() {},
      });
    },
    //删除
    handleDelete(id){
      removeWelcomeById({id: id}).then((res) => {
        if (res.success) {
          this.$message.success(res.message || res.msg);
          this.getSegmentedData();
        } else {
          this.$message.warning(res.message || res.msg);
        }
      });
    },
    modalFormOk() {
      // 新增/修改 成功时，重载列表
      this.getSegmentedData();
    },
    handelecreat(record,nameType) {
      // nameType 复制传1
      this.$router.push({path: '/marketingCenter/code/welcomewords/create', query: {id: record.id}})
    },
    doudi() {
      this.$router.push({path:'/marketingCenter/code/welcomewords/endrule'})
    }
  }
}
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>